package day14;

import java.util.LinkedList;

//使用LinkedList模拟一个队列或堆栈数据结构
public class LinkedListTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Queue<String> queue = new Queue<String>();
		queue.add("java01");
		queue.add("java02");
		queue.add("java03");
		queue.add("java04");
		System.out.println(queue.get());
		System.out.println(queue.get());
		while(!queue.isEmpty()) {
			System.out.println(queue.get());
		}
		System.out.println("*************************");
		Stack<String> stack = new Stack<String>();
		stack.add("java01");
		stack.add("java02");
		stack.add("java03");
		stack.add("java04");
		stack.add("java05");
		while (!stack.isEmpty()) {
			System.out.println(stack.get());
		}
	}

}
//队列：如同一个水管，放球进去，先进先出first in first out
class Queue<E> {
	
	private LinkedList<E> link;
	
	public Queue() {
		link = new LinkedList<E>();
	}
	
	public void add(E e) {
		link.offerFirst(e);
	}
	
	public E get() {
		return link.pollLast();
	}
	
	public boolean isEmpty() {
		return link.isEmpty();
	}
}
//堆栈：如同一个杯子，先进后出，first in last out
class Stack<E> {
	
	private LinkedList<E> link;
	
	public Stack() {
		link = new LinkedList<E>();
	}
	
	public void add(E e) {
		//link.offFirst(e);
		link.push(e);
	}
	
	public E get() {
		//return link.pollFirst();
		return link.pop();
	}
	
	public boolean isEmpty() {
		return link.isEmpty();
	}
}